package com.vitas.coin.resp

import com.snap.base.util.BaseUtil
import com.snap.common.bean.resp.RespBean
import com.snap.http.HttpUtil
import com.snap.http.request.addParam
import com.snap.http.request.asResponse
import com.snap.oss.ktx.uploadVoiceToOss
import com.snap.util.log.SLog
import com.vitas.coin.constant.HttpConstant

/**
 * @description:
 * @author: Funky
 * @date: 2024/10/21 13:02
 */
class UploadResp {


    /**
     * 上传音频文件到oss
     *
     * @param path 本地文件路径
     * @param format 文件格式
     */
    suspend fun requestUpload(path: String, format: String = "mp3",
                              action: (String) -> Unit) {
        BaseUtil.uploadVoiceToOss(
            path = path,
            format = format,
            success = {
                action.invoke(it)
            },
            failed = {
                action.invoke("添加失败")
            }
        )
    }




    /**
     * 创作音频
     *
     *  @param parentId 所属的音频分类Id
     *  @param name 音频名称
     *  @param url 上传到oss后，得到的访问地址
     */
    suspend fun requestSubmitMusic(parentId: Long,
                                   name: String,
                                   url: String,
                                   action: (Boolean) -> Unit) {
        HttpUtil.post(HttpConstant.VOICE_UPLOAD)
            .addParam("parentId", parentId)
            .addParam("name", name)
            .addParam("voiceUrl", url)
            .asResponse<RespBean<Any>>()
            .onSuccess {
                action.invoke(it.code == 200)
            }
            .onFailure {
                SLog.e("error>>>"+it.toString())
                action.invoke(false)
            }
    }
}